package cn.fl.custpay.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.custpay.condition.CmsPayCommQueryCondition;
import cn.fl.custpay.service.ICmmRechargeRecordService;
import cn.fl.custpay.service.ICmsPayCommService;
import cn.fl.custpay.vo.CmsPayCommVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;

/**
 * @Description: 手续费表 控制层
 */
@Controller
@RequestMapping("/custpay/cmsPayComm")
public class CmsPayCommController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsPayCommController.class);

	/**
	 * CmsPayComm 服务层BEAN
	 */
	@Autowired
	private ICmsPayCommService cmsPayCommService;

	@Autowired
	private ICmmRechargeRecordService cmmRechargeRecordService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toCmsPayCommList.do", method = { RequestMethod.GET})
	public ModelAndView toCmsPayCommList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("custpay/cmsPayComm/cmsPayCommList");
		List<KeyValueVO> orglist = this.cmmRechargeRecordService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
		view.addObject("orglist", orglist);
		BaseLogUtils.info(logger, "toCmsPayCommList", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("custpay/cmsPayComm/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 修改页面
	 */
	@RequestMapping(value = "/modifyCmsPayCommPage.do", method = { RequestMethod.GET })
	@ResponseBody
	public ModelAndView modifyCmsPayCommPage(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		ModelAndView view = new ModelAndView("custpay/cmsPayComm/modifyCmsPayComm");
		String id = request.getParameter("id");
		CmsPayCommVO entity = new CmsPayCommVO();
		if(StringUtils.isNotEmpty(id)){
			entity = this.cmsPayCommService.selectById(Long.valueOf(id));
			if(StringUtils.isNotEmpty(entity.getXlMin())){
				entity.setXlMin(String.valueOf(DecimalUtils.convertFenToYuan(Long.valueOf(entity.getXlMin()))));
			}
			if(StringUtils.isNotEmpty(entity.getXlMax())){
				entity.setXlMax(String.valueOf(DecimalUtils.convertFenToYuan(Long.valueOf(entity.getXlMax()))));
			}
		}
		List<KeyValueVO> orglist = this.cmmRechargeRecordService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
		view.addObject("orglist", orglist);
		view.addObject("cmspaycomm",entity);
		return view;
	}
	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initCmsPayCommList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initCmsPayCommList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String isdept = request.getParameter("isdept");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		CmsPayCommQueryCondition qc = new CmsPayCommQueryCondition();
		if (StringUtils.isNotEmpty(isdept)) {
			qc.setIsdept(isdept);
		}
		page.setCondition(qc);
		
		//TODO SOMETHING
		Pager pager = this.cmsPayCommService.searchListPage(page);
		List<CmsPayCommVO> list = (List<CmsPayCommVO>)pager.getResultList();
		for(CmsPayCommVO _vo : list){
			if(StringUtils.isNotEmpty(_vo.getIsdept())){
				_vo.setIsdept(this.cmmRechargeRecordService.searchOrgNm(_vo.getIsdept(), CurrentThreadContext.getFlServiceSysCd()));
			}
			if(StringUtils.isNotEmpty(_vo.getXlMin())){
				_vo.setXlMin(String.valueOf(DecimalUtils.convertFenToYuan(Long.valueOf(_vo.getXlMin()))));
			}
			if(StringUtils.isNotEmpty(_vo.getXlRate())){
				_vo.setXlRate(_vo.getXlRate()+"%");
			}
			if(StringUtils.isNotEmpty(_vo.getXlMax())){
				_vo.setXlMax(String.valueOf(DecimalUtils.convertFenToYuan(Long.valueOf(_vo.getXlMax()))));
			}
			if(StringUtils.isNotEmpty(_vo.getPaySide())){
				Map<String,String> payside = new HashMap<String,String>();
				payside.put("0","狮桥支付");
				payside.put("1","用户支付");
				_vo.setPaySide(payside.get(_vo.getPaySide()));
			}
		}
		ListPageVO<CmsPayCommVO> listPage = new ListPageVO<CmsPayCommVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.cmsPayCommService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			CmsPayCommVO vo = this.cmsPayCommService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		CmsPayCommVO entity = null;
		try {
			entity = new CmsPayCommVO();
			//TODO ADD PARAM
			Long id = this.cmsPayCommService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmsPayCommVO entity = null;
		try {
			entity = new CmsPayCommVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.cmsPayCommService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyCmsPayComm.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyCmsPayComm(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String cmspayId = request.getParameter("cmspayId");
		String isdept = request.getParameter("isdept");
		String xlMin = request.getParameter("xlMin");
		String xlRate = request.getParameter("xlRate");
		String xlMax = request.getParameter("xlMax");
		String paySide = request.getParameter("paySide");
		String usrPayStr = request.getParameter("usrPayStr");
		String sqPayStr = request.getParameter("sqPayStr");
		String freePayStr = request.getParameter("freePayStr");
		DataResultVO result = null;
		CmsPayCommVO entity = null;
		try {
			entity = new CmsPayCommVO();
			if(StringUtils.isNotEmpty(isdept)){
				entity.setIsdept(isdept);
			}
			if(StringUtils.isNotEmpty(xlMin)){
				entity.setXlMin(String.valueOf(DecimalUtils.convertYuanToFen(Double.valueOf(xlMin))));
			}
			if(StringUtils.isNotEmpty(xlRate)){
				entity.setXlRate(xlRate);
			}
			if(StringUtils.isNotEmpty(xlMax)){
				entity.setXlMax(String.valueOf(DecimalUtils.convertYuanToFen(Double.valueOf(xlMax))));
			}
			if(StringUtils.isNotEmpty(paySide)){
				entity.setPaySide(paySide);
			}
			if(StringUtils.isNotEmpty(usrPayStr)){
				entity.setUsrPayStr(usrPayStr);
			}
			if(StringUtils.isNotEmpty(sqPayStr)){
				entity.setSqPayStr(sqPayStr);
			}
			if(StringUtils.isNotEmpty(freePayStr)){
				entity.setFreePayStr(freePayStr);
			}
			if(StringUtils.isNotEmpty(cmspayId)){
				result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
				entity.setId(Long.valueOf(cmspayId));
				entity.setMdfTm(new Date());
				entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				boolean success = this.cmsPayCommService.modify(entity);
				if(!success){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
					BaseLogUtils.info(logger, "modifySysAppVerCfg", "修改失败.[id="+cmspayId+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
				}
			}else{
				result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
				entity.setCrtTm(new Date());
				entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				Long id = this.cmsPayCommService.add(entity);
				if(null == id){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo(SysLogConstant.LOG_CMM_ADD_FAIL);
					BaseLogUtils.info(logger, "modifySysAppVerCfg", "新增失败.[id="+cmspayId+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
				}
			}

		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+cmspayId+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+cmspayId+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 删除
	 */
	@RequestMapping(value = "/removeCmsPayComm.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object removeCmsPayComm(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "removeSysAppVerCfg", "删除数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CmsPayCommVO entity = null;
		try {
			entity = new CmsPayCommVO();
			entity.setId(Long.valueOf(id));
			entity.setIsDel(1);
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			boolean success = this.cmsPayCommService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removeCmsPayComm", "删除数据失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removeCmsPayComm", "删除数据成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeCmsPayComm", "删除数据失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeCmsPayComm", "删除数据失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

